package com.bj.util;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-8-24
 * Time: 下午4:10
 * To change this template use File | Settings | File Templates.
 */
public class ReaderFile {

    public static void main(String[] arge) throws ClassNotFoundException, SQLException, IOException {


        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/whowantstobeamillionaire?useUnicode=true&characterEncoding=utf-8";
        Connection connectMySQL = DriverManager.getConnection(url, "root", "123456");
        Statement statamentMySQL = connectMySQL.createStatement();
        long BATCH_TIME = (new java.util.Date()).getTime();

        List<String> list = FileUtils.readLines(new File("C:/q3.txt"), "utf-8");
//        List<String> list = FileUtils.readLines(new File("C:/Noname1.txt"), "utf-8");

        Iterator iterator = list.iterator();

        while (iterator.hasNext()) {
            String questionName = ((String) iterator.next()).trim();

            if ("".equals(questionName.trim())) {
                continue;
            }

            String answer1 = ((String) iterator.next()).trim();
            String answer2 = ((String) iterator.next()).trim();
            String answer3 = ((String) iterator.next()).trim();
            String answer4 = ((String) iterator.next()).trim();
            String select = ((String) iterator.next()).trim();


            ResultSet nameResultSet = statamentMySQL.executeQuery("SELECT count(1) from T_QUESTION where name = '" + questionName + "' ");
            nameResultSet.first();
            if (nameResultSet.getLong(1) > 0) {
                nameResultSet.close();
                continue;
            }

            long[] answerId = new long[4];
            int index = Integer.valueOf(select) - 1;

            statamentMySQL.execute(" insert into T_QUESTION (name,status,batch_time) values ('" + questionName + "',1," + BATCH_TIME + ")");
            ResultSet resultSet = statamentMySQL.executeQuery("SELECT LAST_INSERT_ID()");
            resultSet.first();
            long questionId = resultSet.getLong(1);
            resultSet.close();

            statamentMySQL.execute(" insert into T_ANSWER (name,QUESTION_ID) values ('" + answer1 + "'," + questionId + ")");
            ResultSet resultSet1 = statamentMySQL.executeQuery("SELECT LAST_INSERT_ID()");
            resultSet1.first();
            answerId[0] = resultSet1.getLong(1);
            resultSet1.close();

            statamentMySQL.execute(" insert into T_ANSWER (name,QUESTION_ID) values ('" + answer2 + "'," + questionId + ")");
            ResultSet resultSet2 = statamentMySQL.executeQuery("SELECT LAST_INSERT_ID()");
            resultSet2.first();
            answerId[1] = resultSet2.getLong(1);
            resultSet2.close();

            statamentMySQL.execute(" insert into T_ANSWER (name,QUESTION_ID) values ('" + answer3 + "'," + questionId + ")");
            ResultSet resultSet3 = statamentMySQL.executeQuery("SELECT LAST_INSERT_ID()");
            resultSet3.first();
            answerId[2] = resultSet3.getLong(1);
            resultSet3.close();

            statamentMySQL.execute(" insert into T_ANSWER (name,QUESTION_ID) values ('" + answer4 + "'," + questionId + ")");
            ResultSet resultSet4 = statamentMySQL.executeQuery("SELECT LAST_INSERT_ID()");
            resultSet4.first();
            answerId[3] = resultSet4.getLong(1);
            resultSet4.close();

            statamentMySQL.execute(" update T_QUESTION set ANSWER_ID = " + answerId[index] + " where id =  " + questionId);

            System.out.println(questionId);

        }

    }


}
